package com.three.crm.adapter;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener;
import com.three.crm.R;
import com.three.crm.bean.FileBean;
import com.three.crm.util.GlideEngine;

import java.util.ArrayList;
import java.util.List;


public class NinePictureAdapter extends BaseQuickAdapter<FileBean, BaseViewHolder> {

    private String type = "";

    public NinePictureAdapter(@Nullable List<FileBean> data) {
        super(R.layout.item_list_img, data);
    }

    public NinePictureAdapter() {
        super(R.layout.item_list_img);
    }

    public void setType(String mtype){
        this.type = mtype;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, FileBean item) {
        String url = item.getUrl();
        helper.addOnClickListener(R.id.iv_delete)
        .setGone(R.id.iv_delete,type.equals("detail") ? false:true);
        Glide.with(mContext).load(url).placeholder(R.mipmap.ic_default_image).into((ImageView) helper.getView(R.id.iv_img));
        ArrayList<LocalMedia> imgList = new ArrayList<>();
        LocalMedia bean = new LocalMedia();
        bean.setPath(url);
        bean.setOriginalPath(url);
        bean.setCompressPath(url);
        bean.setRealPath(url);
        imgList.add(bean);

        FrameLayout fileContainer = helper.getView(R.id.fl_preview);
        fileContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(mContext)
                                .openPreview()
                                .setImageEngine(GlideEngine.createGlideEngine())
                                .setExternalPreviewEventListener(new OnExternalPreviewEventListener() {
                                    @Override
                                    public void onPreviewDelete(int position) {

                                    }

                                    @Override
                                    public boolean onLongPressDownload(Context context, LocalMedia media) {
                                        return false;
                                    }
                                }).startActivityPreview(0, false, imgList);
            }
        });
    }
}
